<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>插件 | Vuex</title>
    <meta name="description" content="Vue.js 的中心化状态管理方案">
    <link rel="icon" href="logo.png">
  <link rel="apple-touch-icon" href="https://vuex.vuejs.org/icons/apple-touch-icon-152x152.png">
  <link rel="mask-icon" href="https://vuex.vuejs.org/icons/safari-pinned-tab.svg" color="#3eaf7c">
  <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
    
    <link rel="preload" href="https://vuex.vuejs.org/assets/css/0.styles.04c31208.css" as="style"><link rel="preload" href="https://vuex.vuejs.org/assets/js/app.92019be7.js" as="script"><link rel="preload" href="https://vuex.vuejs.org/assets/js/106.6c7f9f4f.js" as="script"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/10.9ff0229a.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/100.ff89a77e.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/101.75e31694.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/102.9eb5b6b9.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/103.84f39ee4.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/104.1d8f5eda.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/105.22d02a0b.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/107.df69838a.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/108.57710a8f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/109.0b9350df.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/11.22ec5291.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/110.da276e13.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/111.d271062b.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/112.fff28bd3.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/12.4bb08316.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/13.eb7734ad.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/14.189de3ec.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/15.96301577.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/16.d35e6c41.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/17.6932f877.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/18.14d47735.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/19.f57a3619.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/20.4b3b43ce.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/21.fdbf3e9a.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/22.38a06d07.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/23.20e52ca0.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/24.38c1ec6f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/25.d0749285.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/26.fa0199ca.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/27.104f01d5.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/28.0502b1eb.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/29.a3deb738.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/3.c2431962.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/30.5f61f97c.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/31.c13e234c.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/32.3346e47e.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/33.9552f2b4.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/34.22c87151.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/35.e274dae2.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/36.1f72edfe.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/37.3c8f5a6b.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/38.18505477.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/39.fa1ab3f0.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/4.6114c173.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/40.82b0bf0f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/41.83249a92.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/42.dedbcb2b.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/43.1626a294.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/44.46f3ccfa.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/45.5e63c438.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/46.4837dfdd.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/47.507f6518.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/48.56ef6c75.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/49.14767935.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/5.fea3e8c1.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/50.a2cf5518.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/51.9a6f14ab.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/52.21aa240c.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/53.a7ca4e09.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/54.18643ae3.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/55.48be8fde.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/56.8123f4e5.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/57.3f666e3d.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/58.a7e61993.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/59.8be4dcd4.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/6.e165131f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/60.b6d2d182.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/61.eca98d13.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/62.5ee1323a.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/63.c36ff790.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/64.b063e22d.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/65.d5a56cb7.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/66.32060737.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/67.7b1f4ad9.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/68.61c1fb5a.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/69.9eecf973.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/7.6a20bd14.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/70.5012a640.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/71.76c479ea.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/72.25f5be51.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/73.d31aea65.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/74.98a6bff3.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/75.fa397fe2.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/76.8b008b0b.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/77.280e8204.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/78.ba855657.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/79.58af331f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/8.259b04b8.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/80.a0635feb.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/81.762ec5d1.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/82.0d4604b1.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/83.9d064f79.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/84.72918f87.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/85.0d9821f3.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/86.1631e292.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/87.6c6ced24.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/88.c8dfc6b4.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/89.3e00d819.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/9.e8b16084.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/90.cd40ed9e.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/91.71809249.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/92.aba0a727.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/93.c9b387fd.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/94.5be4c560.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/95.a52fb380.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/96.74307a3e.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/97.0738b41c.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/98.44547c3f.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/99.9a86824d.js"><link rel="prefetch" href="https://vuex.vuejs.org/assets/js/vendors~docsearch.0cca2532.js">
    
  
<link rel="stylesheet" type="text/css" href="plugins.css" media="all">
</head>
<body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a indepth="true" href="index.html" class="home-link router-link-active"><!----> <span class="site-name">Vuex</span></a> <div class="links" style="max-width:nullpx;"><form action="https://vuex.vuejs.org/zh/guide/plugins.html" id="search-form" class="algolia-search-wrapper search-box"><input value="" id="algolia-search-input" class="search-query"></form> <nav class="nav-links can-hide"><div class="nav-item"><a indepth="true" href="default.html" class="nav-link router-link-active">指南</a></div><div class="nav-item"><a indepth="true" href="default_001.html" class="nav-link">API 参考</a></div><div class="nav-item"><a href="https://github.com/vuejs/vuex/releases" target="_top" rel="noopener noreferrer" class="nav-link external">
  更新记录
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">选择语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display: none;"><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/guide/plugins.html" class="nav-link">English</a></li><li class="dropdown-item"><!----> <a indepth="true" href="plugins.html" class="nav-link router-link-exact-active router-link-active">简体中文</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/ja/guide/plugins.html" class="nav-link">日本語</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/ru/guide/plugins.html" class="nav-link">Русский</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/kr/guide/plugins.html" class="nav-link">한국어</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/ptbr/guide/plugins.html" class="nav-link">Português</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/fr/guide/plugins.html" class="nav-link">Français</a></li></ul></div></div> <a href="https://github.com/vuejs/vuex" target="_top" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar"><nav class="nav-links"><div class="nav-item"><a indepth="true" href="default.html" class="nav-link router-link-active">指南</a></div><div class="nav-item"><a indepth="true" href="default_001.html" class="nav-link">API 参考</a></div><div class="nav-item"><a href="https://github.com/vuejs/vuex/releases" target="_top" rel="noopener noreferrer" class="nav-link external">
  更新记录
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">选择语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display: none;"><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/guide/plugins.html" class="nav-link">English</a></li><li class="dropdown-item"><!----> <a indepth="true" href="plugins.html" class="nav-link router-link-exact-active router-link-active">简体中文</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/ja/guide/plugins.html" class="nav-link">日本語</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/ru/guide/plugins.html" class="nav-link">Русский</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/kr/guide/plugins.html" class="nav-link">한국어</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/ptbr/guide/plugins.html" class="nav-link">Português</a></li><li class="dropdown-item"><!----> <a href="https://vuex.vuejs.org/fr/guide/plugins.html" class="nav-link">Français</a></li></ul></div></div> <a href="https://github.com/vuejs/vuex" target="_top" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <ul class="sidebar-links"><li><a indepth="true" href="installation.html" class="sidebar-link">安装</a></li><li><a indepth="true" href="index.html" class="sidebar-link">Vuex 是什么？</a></li><li><a indepth="true" href="default.html" class="sidebar-link">开始</a></li><li><div class="sidebar-group"><p class="sidebar-heading">核心概念 <!----></p> <ul class="sidebar-group-items"><li><a indepth="true" href="state.html" class="sidebar-link">State</a></li><li><a indepth="true" href="getters.html" class="sidebar-link">Getter</a></li><li><a indepth="true" href="mutations.html" class="sidebar-link">Mutation</a></li><li><a indepth="true" href="actions.html" class="sidebar-link">Action</a></li><li><a indepth="true" href="modules.html" class="sidebar-link">Module</a></li></ul></div></li><li><a indepth="true" href="structure.html" class="sidebar-link">项目结构</a></li><li><a indepth="true" href="plugins.html" class="active sidebar-link">插件</a><ul class="sidebar-sub-headers"></ul></li><li><a indepth="true" href="strict.html" class="sidebar-link">严格模式</a></li><li><a indepth="true" href="forms.html" class="sidebar-link">表单处理</a></li><li><a indepth="true" href="testing.html" class="sidebar-link">测试</a></li><li><a indepth="true" href="hot-reload.html" class="sidebar-link">热重载</a></li></ul> </div> <div class="page"> <div class="content"><h1 id="插件"><a href="#插件" aria-hidden="true" class="header-anchor">#</a> 插件</h1> <div class="scrimba"><a href="https://scrimba.com/p/pnyzgAP/cvp8ZkCR" target="_top" rel="noopener noreferrer">在 Scrimba 上尝试这节课</a></div> <p>Vuex 的 store 接受 <code>plugins</code> 选项，这个选项暴露出每次 mutation 的钩子。Vuex 插件就是一个函数，它接收 store 作为唯一参数：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">myPlugin</span> <span class="token operator">=</span> store <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// 当 store 初始化后调用</span>
  store<span class="token punctuation">.</span><span class="token function">subscribe</span><span class="token punctuation">(</span><span class="token punctuation">(</span>mutation<span class="token punctuation">,</span> state<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token comment">// 每次 mutation 之后调用</span>
    <span class="token comment">// mutation 的格式为 { type, payload }</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><p>然后像这样使用：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> store <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vuex<span class="token punctuation">.</span>Store</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token comment">// ...</span>
  plugins<span class="token punctuation">:</span> <span class="token punctuation">[</span>myPlugin<span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><h3 id="在插件内提交-mutation"><a href="#在插件内提交-mutation" aria-hidden="true" class="header-anchor">#</a> 在插件内提交 Mutation</h3> <p>在插件中不允许直接修改状态——类似于组件，只能通过提交 mutation 来触发变化。</p> <p>通过提交 mutation，插件可以用来同步数据源到 store。例如，同步 websocket 数据源到 store（下面是个大概例子，实际上 <code>createPlugin</code> 方法可以有更多选项来完成复杂任务）：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">function</span> <span class="token function">createWebSocketPlugin</span> <span class="token punctuation">(</span>socket<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> store <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    socket<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'data'</span><span class="token punctuation">,</span> data <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      store<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">'receiveData'</span><span class="token punctuation">,</span> data<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
    store<span class="token punctuation">.</span><span class="token function">subscribe</span><span class="token punctuation">(</span>mutation <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>mutation<span class="token punctuation">.</span>type <span class="token operator">===</span> <span class="token string">'UPDATE_DATA'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        socket<span class="token punctuation">.</span><span class="token function">emit</span><span class="token punctuation">(</span><span class="token string">'update'</span><span class="token punctuation">,</span> mutation<span class="token punctuation">.</span>payload<span class="token punctuation">)</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> plugin <span class="token operator">=</span> <span class="token function">createWebSocketPlugin</span><span class="token punctuation">(</span>socket<span class="token punctuation">)</span>

<span class="token keyword">const</span> store <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vuex<span class="token punctuation">.</span>Store</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  state<span class="token punctuation">,</span>
  mutations<span class="token punctuation">,</span>
  plugins<span class="token punctuation">:</span> <span class="token punctuation">[</span>plugin<span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><h3 id="生成-state-快照"><a href="#生成-state-快照" aria-hidden="true" class="header-anchor">#</a> 生成 State 快照</h3> <p>有时候插件需要获得状态的“快照”，比较改变的前后状态。想要实现这项功能，你需要对状态对象进行深拷贝：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">myPluginWithSnapshot</span> <span class="token operator">=</span> store <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> prevState <span class="token operator">=</span> _<span class="token punctuation">.</span><span class="token function">cloneDeep</span><span class="token punctuation">(</span>store<span class="token punctuation">.</span>state<span class="token punctuation">)</span>
  store<span class="token punctuation">.</span><span class="token function">subscribe</span><span class="token punctuation">(</span><span class="token punctuation">(</span>mutation<span class="token punctuation">,</span> state<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> nextState <span class="token operator">=</span> _<span class="token punctuation">.</span><span class="token function">cloneDeep</span><span class="token punctuation">(</span>state<span class="token punctuation">)</span>

    <span class="token comment">// 比较 prevState 和 nextState...</span>

    <span class="token comment">// 保存状态，用于下一次 mutation</span>
    prevState <span class="token operator">=</span> nextState
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><p><strong>生成状态快照的插件应该只在开发阶段使用</strong>，使用 webpack 或 Browserify，让构建工具帮我们处理：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> store <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vuex<span class="token punctuation">.</span>Store</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token comment">// ...</span>
  plugins<span class="token punctuation">:</span> process<span class="token punctuation">.</span>env<span class="token punctuation">.</span><span class="token constant">NODE_ENV</span> <span class="token operator">!==</span> <span class="token string">'production'</span>
    <span class="token operator">?</span> <span class="token punctuation">[</span>myPluginWithSnapshot<span class="token punctuation">]</span>
    <span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><p>上面插件会默认启用。在发布阶段，你需要使用 webpack 的 <a href="https://webpack.js.org/plugins/define-plugin/" target="_top" rel="noopener noreferrer">DefinePlugin<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a> 或者是 Browserify 的 <a href="https://github.com/hughsk/envify" target="_top" rel="noopener noreferrer">envify<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a> 使 <code>process.env.NODE_ENV !== 'production'</code> 为 <code>false</code>。</p> <h3 id="内置-logger-插件"><a href="#内置-logger-插件" aria-hidden="true" class="header-anchor">#</a> 内置 Logger 插件</h3> <blockquote><p>如果正在使用 <a href="https://github.com/vuejs/vue-devtools" target="_top" rel="noopener noreferrer">vue-devtools<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>，你可能不需要此插件。</p></blockquote> <p>Vuex 自带一个日志插件用于一般的调试:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> createLogger <span class="token keyword">from</span> <span class="token string">'vuex/dist/logger'</span>

<span class="token keyword">const</span> store <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vuex<span class="token punctuation">.</span>Store</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  plugins<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token function">createLogger</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><p><code>createLogger</code> 函数有几个配置项：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> logger <span class="token operator">=</span> <span class="token function">createLogger</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  collapsed<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token comment">// 自动展开记录的 mutation</span>
  <span class="token function">filter</span> <span class="token punctuation">(</span>mutation<span class="token punctuation">,</span> stateBefore<span class="token punctuation">,</span> stateAfter<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 若 mutation 需要被记录，就让它返回 true 即可</span>
    <span class="token comment">// 顺便，`mutation` 是个 { type, payload } 对象</span>
    <span class="token keyword">return</span> mutation<span class="token punctuation">.</span>type <span class="token operator">!==</span> <span class="token string">"aBlacklistedMutation"</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token function">transformer</span> <span class="token punctuation">(</span>state<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 在开始记录之前转换状态</span>
    <span class="token comment">// 例如，只返回指定的子树</span>
    <span class="token keyword">return</span> state<span class="token punctuation">.</span>subTree
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token function">mutationTransformer</span> <span class="token punctuation">(</span>mutation<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// mutation 按照 { type, payload } 格式记录</span>
    <span class="token comment">// 我们可以按任意方式格式化</span>
    <span class="token keyword">return</span> mutation<span class="token punctuation">.</span>type
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  logger<span class="token punctuation">:</span> console<span class="token punctuation">,</span> <span class="token comment">// 自定义 console 实现，默认为 `console`</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><p>日志插件还可以直接通过 <code>&lt;script&gt;</code> 标签引入，它会提供全局方法 <code>createVuexLogger</code>。</p> <p>要注意，logger 插件会生成状态快照，所以仅在开发环境使用。</p></div> <div class="page-edit"><!----> <!----></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a indepth="true" href="structure.html" class="prev">
          项目结构
        </a></span> <span class="next"><a indepth="true" href="strict.html">
          严格模式
        </a>
        →
      </span></p></div> <div class="bsa-cpc-wrapper"><div class="bsa-cpc"></div></div></div> <!----></div></div>
    
  




</body></html>
